﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using STT.Business;
using STT.Entities;
namespace STT.Admin.UserControls
{
    public partial class ucGiangVien : System.Web.UI.UserControl
    {
        public bool chonSuaClick;
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadGiangVien();
            Loanddl();
            btSua.Visible = false;
            btThemMoi.Visible = true;
        }
        void Loanddl()
        {
            ddChucVu.DataSource = BTbChucVu.SelectAll();
            ddChuyenMon.DataSource = BTbTDChuyenMon.SelectAll();
            ddDonVi.DataSource = BTbDonVi.SelectAll();
            ddHocVan.DataSource = BTbTDHocVan.SelectAll();


            ddChucVu.DataTextField = "TenChucVu";
            ddChuyenMon.DataTextField = "TenTDChuyenMon";
            ddDonVi.DataTextField = "TenDonVi";
            ddHocVan.DataTextField = "TenTDHocVan";

            ddChucVu.DataValueField = "MaChucVu";
            ddChuyenMon.DataValueField = "MaTDChuyenMon";
            ddDonVi.DataValueField = "MaDonVi";
            ddHocVan.DataValueField = "MaTDHocVan";

            ddChucVu.DataBind();
            ddChuyenMon.DataBind();
            ddDonVi.DataBind();
            ddHocVan.DataBind();

        }
        void LoadGiangVien()
        {
            gvGiangVien.AutoGenerateColumns = false;
            gvGiangVien.DataSource = BTbGiangVien.SelectAll();
            gvGiangVien.DataBind();
        }
        bool KiemTraDuLieu()
        {
            return BTbGiangVien.KiemTraDuLieu(new ETbGiangVien
            {
             Sdt=txtSDT.Text
            });
        }
        void ThongBao(string Loi)
        {
            ScriptManager.RegisterClientScriptBlock(this, GetType(), "Thông báo!", "<script>alert('" + Loi + "')</script>", false);
        }
        protected void gvGiangVien_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvGiangVien.EditIndex = e.NewEditIndex;
            gvGiangVien.DataBind();
        }

        protected void gvGiangVien_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                int MaGV = int.Parse(gvGiangVien.DataKeys[e.RowIndex].Value.ToString());

                BTbGiangVien.Delete(MaGV);
                LoadGiangVien();
            }
            catch
            {
                ThongBao("Dữ liệu liên quan đến Khóa học này vẫn tồn tại, vui lòng kiểm tra lại dữ liệu!");
            }
        }

        protected void gvGiangVien_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvGiangVien.EditIndex = -1;
            gvGiangVien.DataBind();
        }

        protected void btThemMoi_Click(object sender, EventArgs e)
        {
            if (!KiemTraDuLieu())
            {
                BTbGiangVien.Insert(new ETbGiangVien
                {
                   TenGV= txtTenGiangVien.Text,
                   Sdt=txtSDT.Text,
                   DiaChi=txtDiaChi.Text,
                   MaDonVi=Int32.Parse(ddDonVi.SelectedValue),
                   MaChucVu=Int32.Parse(ddChucVu.SelectedValue),
                   SoCMT=txtSoCMT.Text,
                   MaTDChuyenMon=Int32.Parse(ddChuyenMon.SelectedValue),
                   MaTDHocVan=Int32.Parse(ddHocVan.SelectedValue),
                   GhiChu=txtGhiChu.Text

                });

                LoadGiangVien();
            }
            else
            {
                ThongBao("Khóa học này đã tồn tại!");

            }
        }

        protected void lbXuatDuLieu_Click(object sender, EventArgs e)
        {

        }

        protected void lbXoaTat_Click(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i < gvGiangVien.Rows.Count; i++)
                {
                    CheckBox cbChon = (CheckBox)gvGiangVien.Rows[i].Cells[0].FindControl("cbChon");
                    if (cbChon != null && cbChon.Checked)
                    {
                        BTbGiangVien.Delete(int.Parse(gvGiangVien.DataKeys[i].Value.ToString()));
                    }
                }

                LoadGiangVien();
            }
            catch
            {
                ThongBao("Dữ liệu liên quan đến Khóa học này vẫn tồn tại, vui lòng kiểm tra lại dữ liệu!");
            }
        }

        // Phân trang
        protected void gvGiangVien_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvGiangVien.PageIndex = e.NewPageIndex;
            gvGiangVien.DataBind();
        }

        protected void gvGiangVien_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "ChonSua")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                 
                // Thay đổi tiêu đề
                txtTieuDe.InnerText = "Sửa khóa học";
                txtTenGiangVien.Text = gvGiangVien.Rows[index].Cells[2].Text;
                txtSDT.Text = gvGiangVien.Rows[index].Cells[3].Text;
                txtDiaChi.Text = gvGiangVien.Rows[index].Cells[4].Text;
                ddDonVi.Text = gvGiangVien.Rows[index].Cells[5].Text;
                ddChucVu.Text = gvGiangVien.Rows[index].Cells[6].Text;
                txtSoCMT.Text = gvGiangVien.Rows[index].Cells[7].Text;
                ddChuyenMon.Text = gvGiangVien.Rows[index].Cells[8].Text;
                ddHocVan.Text = gvGiangVien.Rows[index].Cells[9].Text;
                txtGhiChu.Text = gvGiangVien.Rows[index].Cells[10].Text;
                btSua.Visible = true;
                btThemMoi.Visible = false;
                chonSuaClick = true;
            }
        }

        protected void btSua_Click(object sender, EventArgs e)
        {
            BTbGiangVien.Update(new ETbGiangVien
            {
                TenGV = txtTenGiangVien.Text,
                Sdt = txtSDT.Text,
                DiaChi = txtDiaChi.Text,
                MaDonVi = Int32.Parse(ddDonVi.SelectedValue),
                MaChucVu = Int32.Parse(ddChucVu.SelectedValue),
                SoCMT = txtSoCMT.Text,
                MaTDChuyenMon = Int32.Parse(ddChuyenMon.SelectedValue),
                MaTDHocVan = Int32.Parse(ddHocVan.SelectedValue),
                GhiChu = txtGhiChu.Text
            });
            LoadGiangVien();
        }

        protected void btSearch_Click(object sender, ImageClickEventArgs e)
        {
            BTbGiangVien.SearchTenGiangVien(new ETbGiangVien
            {
               TenGV = txtTimKiem.Text
            });
            LoadGiangVien();
        }

        protected void btImport_Click(object sender, EventArgs e)
        {
          
        }
        protected void btThemExcel_Click(object sender, EventArgs e)
        {

        }

       

    }
}